/**
 * snapp.admin package
 */
snapp.admin = {
	// TODO - Implement package
};

/**
 * snapp.admin.AdminComponent class
 * 
 * This class defines an extensible admin component
 */
snapp.admin.AdminComponent = new Class({
   
   	Implements: Options,
   	
   	options: {
   		name: null, 		// required
   		formURI: null, 		// required
		formID: null,		// required
		objectClass: null,	// required
		listSecPermit: null, // optional
		editSecPermit: null, // optional
		editWindowClass: layout.SNAppWindow, // optional
		listWindowClass: layout.SNAppWindow, // optional
		// edit window options - optional
		editWindowOptions: {
			width: 500,
			height: 500
		},

		listWindowOptions: {
			width: 300,
			height: 400
		},
		
		listFilter: {
			/*
			 * Filter options for object list.
			 * 
			 * Can be static properties (status:1) 
			 * 
			 * To use dynamically driven properties, overwrite the "list()" method
			 */
		},
		
		listOptions: {
			labelProperty: 'name',
			cssClass: 'fake_a'	
		}
   	},
   	
   	initialize: function(options) {
   		this.setOptions(options);
   		this.options.editWindowOptions.onClose = function() {
			this.editWindow = null;
		}.bind(this);
			
   		this.options.listWindowOptions.onClose = function() {
			this.listWindow = null;
		}.bind(this);
   	},
   	                      

	currentObject: null,
	currentList: null,
	
	editWindow: null,
	listWindow: null,
	
	getEditWindow: function() {
		if (typeOf(this.editWindow) == 'null') {
			this.editWindow = new this.options.editWindowClass(this.options.editWindowOptions);
			this.editWindow.create();
		}
		return this.editWindow;
	},
	
	getNewObject: function() {
		obj = new this.options.objectClass({
			events:{
				save: this.saveHandler.bind(this),
				load: this.loadHandler.bind(this),
				list: this.listHandler.bind(this)
			}
		});
		obj.setProperty('id',0);
		return obj;
	},
	
	getListWindow: function() {
		if (!this.listWindow) {
			this.listWindow = new this.options.listWindowClass(this.options.listWindowOptions);
			this.listWindow.create();
		}
		return this.listWindow;
	},
	
	create: function() {
		this.edit(0);
		$(this.options.formID).reset();
	},
	
	edit: function(oid) {
		this.currentObject = this.getNewObject();
		
		this.currentObject.setProperty('id',oid);
		this.loadOpts = {
			'id':oid
		};
		if (this.options.editSecPermit != null){
			this.loadOpts.secCheck = snapp.session.access.getSecCheck(this.options.editSecPermit);
		}

		if (typeOf($(this.options.formID)) == 'null') {
			this.getEditWindow().load(this.options.formURI,{
				onSuccess: function() {
					if (this.currentObject.getProperty('id') > 0) {
						this.currentObject.load(this.loadOpts);
					}
				}.bind(this)
			});
		} else {
			if (this.currentObject.getProperty('id') > 0) {
				this.currentObject.load(this.loadOpts);
			} else {
				$(this.options.formID).reset();
			}
		}
		
		this.editWindow.focus();
	},

	loadHandler: function() {
		this.currentObject.toForm($(this.options.formID));
	},
	
	save: function(frm) {
		try {
			if (this.options.editSecPermit != null){
				this.currentObject.setProperty('secCheck',snapp.session.access.getSecCheck(this.options.editSecPermit));
			}
			this.currentObject.save(frm);
		
		} catch(e) {
			alert("Error saving object: "+e);
		}
	},
	
	saveHandler: function() {
		$(this.options.formID).reset();
		this.currentObject = this.getNewObject();
		this.list();
	},
	
	list: function() {
		listProps = this.options.listFilter;
		//TODO - Overwrite permission 
		if (this.options.listSecPermit != null &&
			snapp.session.access.hasPermit(this.options.listSecPermit)) {
			listProps.secCheck = snapp.session.access.getSecCheck(this.options.listSecPermit);
		}
			
		this.currentList = this.getNewObject();
		this.currentList.list(listProps);
			
	},
	
	listHandler: function() {
		//alert('building list');
		this.listWindow = this.getListWindow();
		this.listWindow.empty();
		
		content = this.listWindow.getDefaultTemplate();
		
		lwrapper = new Element('ul',{styles:{'text-align':'left'}}).inject(content.main);
		this.currentList.each(function(listitem) {
			lwrapper.grab(new Element('li',{
					id: this.options.name+'-'+listitem.getProperty('id'),
					text: listitem.getProperty(this.options.listOptions.labelProperty),
					class: this.options.listOptions.cssClass,
					styles:{
						'text-align':'left'
					}
				})
				.store('oid',listitem.getProperty('id'))
				.addEvent('click',function(e) {
					this.edit($(new Event(e).target).retrieve('oid'));	
				}.bind(this))
			);
		}.bind(this));
		
		content.actions.grab(
			new Element('button',{'text':'Create New','class':'small_button','style':'bottom:0px'})
			.addEvent('click',function(){
				this.create()	
			}.bind(this))
		);
		this.listWindow.focus();
	}
	
});
